package com.hollycrm.hollysqm.analyzer.dic;

import com.hollycrm.hollysqm.analyzer.util.ConfigUtil;
import com.hollycrm.hollysqm.analyzer.util.Constant;
import com.hollycrm.hollysqm.analyzer.util.JdbcCallback;
import com.hollycrm.hollysqm.analyzer.util.JdbcUtil;
import lombok.extern.log4j.Log4j;
import org.ansj.library.UserDefineLibrary;
import org.ansj.util.FilterModifWord;
import org.apache.commons.lang.StringUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhaihw on 2016/5/4.
 * 加载词库
 */
@Log4j
public class DictionaryLoader {

    /**
     * 私有化
     */
    private DictionaryLoader() {
    }

    /**
     * 加载器
     */
    private static DictionaryLoader loader = new DictionaryLoader();

    /**
     * 获取实例
     *
     * @return
     */
    public static DictionaryLoader getInstance() {
        return loader;
    }

    /**
     * 加载停用词
     */
    public void loadStopDic() throws SQLException, ClassNotFoundException {
        int b = 1, size = 1000;
        String stopSql = ConfigUtil.getProp(Constant.stopSql);
        Connection connection = null;
        try {
            connection = JdbcUtil.INSTANCE.getConnection();
            List<String> words;
            do {
                words = loadDB(connection, stopSql, new Object[]{size * b, (b - 1) * size}); //加载停用词
                b++;
                log.info("load stop dic: size=" + words.size());
                FilterModifWord.insertStopWords(words); //分词器加载
                Dictionary.loadStopWords(words); //私有词典加载
            } while (words.size() != 0);

        } catch (SQLException | ClassNotFoundException e) {
            throw e;
        } finally {
            try {
                if (connection != null)
                    connection.close();
            } catch (Exception e) {
                log.warn(e);
            }
        }

    }

    /**
     * 加载用户词典
     */
    public void loadUserDic() throws SQLException, ClassNotFoundException {
        int b = 1, size = 1000;
        String userSql = ConfigUtil.getProp(Constant.userSql);
        Connection connection = null;
        try {
            connection = JdbcUtil.INSTANCE.getConnection();
            List<String> words;
            UserDefineLibrary.clear(); //清空词库
            do {
                words = loadDB(connection, userSql, new Object[]{size * b, (b - 1) * size}); //加载停用词
                b++;
                log.info("load user dic: size=" + words.size());
                for (String word : words) {
                    Dictionary.getUserDictWords().add(word);
                    UserDefineLibrary.insertWord(word, "un", 1000);
                }
            } while (words.size() != 0);
        } catch (SQLException | ClassNotFoundException e) {
            throw e;
        } finally {
            try {
                if (connection != null)
                    connection.close();
            } catch (Exception e) {
                log.warn(e);
            }
        }
    }

    /**
     * 获取数据库中数据
     *
     * @param conn   连接对象
     * @param sql    sql
     * @param params 参数
     * @return 对象列表
     * @throws SQLException           异常
     * @throws ClassNotFoundException 驱动类异常
     */
    public List<String> loadDB(Connection conn, String sql, Object[] params) throws SQLException, ClassNotFoundException {
        return JdbcUtil.INSTANCE.executeQuery(conn, sql, params, new JdbcCallback<List<String>>() {
            @Override
            public List<String> process(ResultSet rs) throws SQLException {
                List<String> list = new ArrayList<String>();
                while (rs.next()) {
                    String word = rs.getString(1);
                    if(StringUtils.isNotEmpty(word))
                        list.add(word);
                }
                return list;
            }
        });
    }

}
